import Vue from 'vue'
import Vuex from "vuex"
import { INCREMENT_MUTATION, DECREMENT_MUTATION,MULTIPLY_MUTATION } from '../store/mutation-types'
import { INCREMENT_ACTION,DECREMENT_ACTION,MULTIPLY_ACTION } from '../store/action-types' 

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        count: 0
    },
    mutations: {
        [INCREMENT_MUTATION](state, data){
            // state.count += data;
            state.count += data.payload;
        },

        [DECREMENT_MUTATION](state, data){
            state.count -= data.payload;
        },

        [MULTIPLY_MUTATION](state, data){
            state.count *= data.payload;
        }
    },
    actions:{
        [INCREMENT_ACTION]({ commit }, action){
            console.log(action);
            return new Promise((resolve) => {
                setTimeout(() => {
                    commit({
                        type: INCREMENT_MUTATION,
                        payload: action.payload
                    })
                    resolve()
                },2)
            })
        },
        [DECREMENT_ACTION]({commit}, action){
            setTimeout(() => {
                commit({ 
                    type: DECREMENT_MUTATION,
                    payload: action.payload
                })
            },1)
        },
        async [MULTIPLY_ACTION](context, action){
            console.log("1");
            console.log(action);

            await context.dispatch({
                type: INCREMENT_MUTATION,
                payload: action.payload.inc
            })

            console.log("2");
            console.log(action);

            context.commit({
                type: MULTIPLY_MUTATION,
                payload: action.payload.mul
            })
        }
    },
    getters: {
        doubleCount(state){
            return state.count * 2
        },

        tripleCount(state){
            return state.count * 3
        },
        // addCount(state, getters){
        //     return getters.doubleCount + getters.tripleCount
        //     // return (num) => {
        //     //     return num + getters.doubleCount + getters.tripleCount;
        //     // }
        // }
    }

})

export default store